﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace MilitaryPluginInterface
{
    public class HealthPotion : VisibleGameEntity
    {
        private int _health;
        private int _maxHealth;
        private int _width;
        private Texture2D _healthTexture;
        private Texture2D _regenningTexture;
        private bool _isRegenning = false;
        private bool _isDrawRegenning = false;

        public int MaxHealth
        {
            get { return _maxHealth; }
            set { _maxHealth = value; }
        }

        public int Health
        {
            get { return _health; }
            set 
            {
                if (value <= MaxHealth)
                    _health = value;
                else
                    _health = MaxHealth;
            }
        }

        public bool IsRegenning
        {
            get { return _isRegenning; }
            set { _isRegenning = value; }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public HealthPotion(string strImage, int health)
        {
            _health = health;
            _maxHealth = health;
            _healthTexture = Constant.content.Load<Texture2D>(strImage);
            _regenningTexture = Constant.content.Load<Texture2D>(strImage + @"_regenning");
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color colorz)
        {
            float scalePotion = getRemainHealth();
            int newWidth = (int)(scalePotion * _width);
            Color color;
            if (scalePotion >= 0.75f)
                color = Color.LightGreen;
            else if (scalePotion >= 0.5f)
                color = Color.Orange;
            else if (scalePotion >= 0.25f)
                color = Color.OrangeRed;
            else
                color = Color.Red;
            spriteBatch.Draw(_healthTexture, new Rectangle((int)Position.X, (int)Position.Y, newWidth, _healthTexture.Height), color);

            if (IsRegenning && _isDrawRegenning)
                spriteBatch.Draw(_regenningTexture, new Vector2(Position.X - 15, Position.Y - _regenningTexture.Height/2), Color.White);
        }

        public override void update(GameTime gameTime)
        {
            if (IsRegenning)
            {
                _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
                if (_currentTotalTime > Constant.regenTime * 1000)
                {
                    _isDrawRegenning = !_isDrawRegenning;
                    _currentTotalTime = 0;
                    regenHealth();
                }
            }
            else
                _isDrawRegenning = false;
        }

        public float getRemainHealth()
        {
            return (float)_health / _maxHealth;
        }

        public void regenHealth()
        {
            float regenValue = MaxHealth * 0.02f;
            Health += (int)regenValue;
        }
    }
}
